package com.resou.reader.paycenter.p;

import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.VIPConfigBean;
import com.resou.reader.api.service.PayService;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.paycenter.v.IVIPCenterView;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ${Garrett} on 2018/9/19.
 * Contact me krouky@outlook.com
 */
public class VIPCenterPresenter extends BasePresenter<IVIPCenterView, IModel> {

    private static final String TAG = "VIPCenterPresenter";
    private PayService mService;

    private List<VIPConfigBean> configs;

    public VIPCenterPresenter(IVIPCenterView view) {
        this.view = view;
        mService = ApiImp.getInstance().getService(PayService.class);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        requestConfigs();
    }

    private void requestConfigs() {
        mService.vipPriceConfig().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .as(this.<Result<List<VIPConfigBean>>>bindLifecycle())
                .subscribe(new Consumer<Result<List<VIPConfigBean>>>() {
                    @Override
                    public void accept(Result<List<VIPConfigBean>> listResult) throws Exception {
                        DLog.i(TAG, "vipPriceConfig success ,code:" + listResult.getCode());
                        configs = listResult.getData();
                        view.refreshConfig(configs);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        DLog.i(TAG, "vipPriceConfig error ,mes:" + throwable.getMessage());
                        throwable.printStackTrace();
                    }
                });
    }
}
